package com.lxx.common;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import static com.lxx.utils.Constant.SESSION_USER_FLAG;

import com.lxx.modules.sys.entity.SysUser;
import com.lxx.utils.SessionInfo;

public class BaseAction {
	@Autowired
	protected HttpSession session;
	
	// 
	/**
	 * 判断当前用户是否已登陆
	 * @param session
	 * @return
	 */
	protected boolean isLogged(HttpSession session) {
		Object o = session.getAttribute(SESSION_USER_FLAG);
		return o != null;
	}
	
	protected void saveUser2Session(HttpSession session, SysUser user) {
		SessionInfo sessionInfo = new SessionInfo();
		sessionInfo.setUser(user);
		session.setAttribute(SESSION_USER_FLAG, sessionInfo);
	}
	
	protected void invalidateSession() {
		session.invalidate();
	}
	
	protected void invalidateSession(HttpSession session) {
		session.invalidate();
	}
	
	protected SysUser sessionUser() {
		SessionInfo o = (SessionInfo) session.getAttribute(SESSION_USER_FLAG);
		return o.getUser();
	}
	
	protected SysUser sessionUser(HttpSession session) {
		SessionInfo o = (SessionInfo) session.getAttribute(SESSION_USER_FLAG);
		return o.getUser();
	}

}
